layui.config({
    base: ctx + 'static/lib/layui_exts/dtree/' //配置 layui 第三方扩展组件存放的基础目录
}).extend({
    dtree: 'dtree' //定义该组件模块名
}).use(['element','layer','table','dtree'],  function () {
    var dtree = layui.dtree
    ,form = layui.form;
    layer = layui.layer;

    var DTree = dtree.render({
        elem: "#menu-tree",  //绑定元素
        url: ctx + 'sys/menu/tree',
        checkbar: true,
        async: false,
        initLevel: 1
    });
    form.on('submit(add)', function (data) {
        console.log(data.field);
        var params = dtree.getCheckbarNodesParam("menu-tree");
        var ids = _.map(params, function (item) {
            return item.nodeId;
        });
        data.field['menuIdList'] = ids;
        axios.post(ctx + 'sys/role/save', data.field).then(function (res) {
            if(res.data.code == 0) {
                layer.msg('保存成功');
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                parent.table.reload();
            }else {
                layer.msg('保存失败');
            }
        }).catch(function (err) {
           console.log(err);
        });
        return false;
    });
});

$('#cancel').click(function () {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
});